package com.jky.trial.service.impl;

import com.jky.trial.common.ListResult;
import com.jky.trial.common.Pagination;
import com.jky.trial.entity.Msg;
import com.jky.trial.entity.MsgExample;
import com.jky.trial.mapper.MsgMapper;
import com.jky.trial.service.MsgService;
import com.jky.trial.vo.MsgVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MsgServiceImpl implements MsgService {
    private final MsgMapper recordMapper;


    @Autowired
    MsgServiceImpl(MsgMapper recordMapper) {
        this.recordMapper = recordMapper;
    }

    //    @Override
//    public Msg findOne(Msg record) {
//        MsgExample recordExample = new MsgExample();
//        MsgExample.Criteria criteria = recordExample.createCriteria();
//        if (record.getId() != null) criteria.andIdEqualTo(record.getId());//查询条件
////        if (record.getName() != null) criteria.andNameEqualTo(record.getName());
////        if (record.getPassword() != null) criteria.andPasswordEqualTo(record.getPassword());
////        if (record.getPhone() != null) criteria.andPhoneEqualTo(record.getPhone());
//        if (record.getStatus() != null) criteria.andStatusEqualTo(record.getStatus());
//        List<Msg> Msgs = recordMapper.selectByExample(recordExample);
//        return Msgs != null ? Msgs.get(0) : null;
//    }
    @Override
    public int create(Msg record) {
//        record.setCreateAt(new Date());
        return recordMapper.insertSelective(record);
    }

    @Override
    public Msg sendMsg(int fromUser, int toUser, String content, int targetId, int type) {
//        if (toUser == fromUser) return null;//自己不能够发送给自己
        Msg record = new Msg();
        record.setToUser(toUser);
        record.setContent(content);
        record.setTargetId(targetId);
        record.setFromUser(fromUser);
        record.setType(type);
        this.create(record);
        return record;
    }
//    @Override
//    public Msg sendMsg(int toUser, String content) {
//        Msg record = new Msg();
//        record.setToUser(toUser);
//        record.setContent(content);
//        this.create(record);
//        return record;
//    }

    @Override
    public int update(Msg record) {
        MsgExample recordExample = new MsgExample();
        MsgExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    @Override
    public int del(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delBy(Msg msg) {
        if (msg.getTargetId() == null && msg.getType() == null && msg.getToUser() == null) return 0;
        return recordMapper.delBy(msg);
    }

    @Override
    public int delBy(int targetId) {
        Msg msg = new Msg();
        msg.setTargetId(targetId);
        return recordMapper.delBy(msg);
    }

    @Override
    public Msg detail(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MsgVo> list(MsgVo record) {
        return recordMapper.pageList(record);
    }

    @Override
    public ListResult page(MsgVo record) {
        List<MsgVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public long count(Msg record) {
        MsgExample recordExample = new MsgExample();
        MsgExample.Criteria criteria = recordExample.createCriteria();
        if (record.getSearch() != null) { //按名称搜索
            criteria.andContentLike("%" + record.getSearch());
        }
        criteria.andToUserEqualTo(record.getToUser());
        if (record.getStatus() != null) { //按状态筛选
            criteria.andStatusEqualTo(record.getStatus());
        }
        return recordMapper.countByExample(recordExample);
    }

    @Override
    public ListResult pageListWithFromUserName(MsgVo example) {
        example.setDeleted(false);
        List<MsgVo> list = recordMapper.pageListWithFromUserName(example);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                example.getPage(),
                (int) count(example),
                example.getSize()
        ));
        return res;
    }
}
